<template>
  <!-- 数据采集流程节点表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
    destroy-on-close
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="采集流程主键" prop="flowId">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.flowId"
              placeholder="请输入采集流程主键"
              clearable
            />

            <span v-else>{{ formData.flowId }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="节点编号" prop="nodeCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.nodeCode"
              placeholder="请输入节点编号"
              clearable
            />

            <span v-else>{{ formData.nodeCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="节点名称" prop="nodeName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.nodeName"
              placeholder="请输入节点名称"
              clearable
            />

            <span v-else>{{ formData.nodeName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="节点类型" prop="nodeType">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.nodeType"
              placeholder="请输入节点类型"
              clearable
            />

            <span v-else>{{ formData.nodeType }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="节点设计参数" prop="nodeDesignParam">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.nodeDesignParam"
              placeholder="请输入节点设计参数"
              clearable
            />

            <span v-else>{{ formData.nodeDesignParam }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="处理器类型" prop="processorType">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.processorType"
              placeholder="请选择处理器类型"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('processorType')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName(
                "processorType",
                formData.processorType
              )
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="输入主题" prop="inputTopic">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.inputTopic"
              placeholder="请输入输入主题"
              clearable
            />

            <span v-else>{{ formData.inputTopic }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="输出主题" prop="outputTopic">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.outputTopic"
              placeholder="请输入输出主题"
              clearable
            />

            <span v-else>{{ formData.outputTopic }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />

            <span v-else>{{ formData.remarks }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "ADMINSCACOLLCETFLOWNODEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  flowId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  nodeCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  nodeName: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  nodeType: [
    { min: 1, max: 20, message: "长度在 1 到 20 个字符", trigger: "blur" },
  ],
  nodeDesignParam: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  processorType: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  inputTopic: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  outputTopic: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  flowId: "",
  nodeCode: "",
  nodeName: "",
  nodeType: "",
  nodeDesignParam: "",
  processorType: "",
  inputTopic: "",
  outputTopic: "",
  prevNodeIds: "",
  nextNodeIds: "",
  deleted: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        flowId,
        nodeCode,
        nodeName,
        nodeType,
        nodeDesignParam,
        processorType,
        inputTopic,
        outputTopic,
        prevNodeIds,
        nextNodeIds,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        flowId,
        nodeCode,
        nodeName,
        nodeType,
        nodeDesignParam,
        processorType,
        inputTopic,
        outputTopic,
        prevNodeIds,
        nextNodeIds,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.flowId = "";
  formData.nodeCode = "";
  formData.nodeName = "";
  formData.nodeType = "";
  formData.nodeDesignParam = "";
  formData.processorType = "";
  formData.inputTopic = "";
  formData.outputTopic = "";
  formData.prevNodeIds = "";
  formData.nextNodeIds = "";
  formData.deleted = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 数据采集流程节点保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/iotadmin/scacollcetflownode/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
